package com.wmyg.antifake.controller;

import com.wmyg.antifake.entity.BkUser;
import com.wmyg.antifake.service.BkUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-05-18
 */
@RestController
public class UserController {

    @Autowired
    BkUserService bkUserService;

    @GetMapping(value = "/me")
    public BkUser me(Principal principal) {
        return bkUserService.findByUsername(principal.getName());
    }

    @PutMapping(value = "/password")
    public Boolean changePassword(String password, Principal principal) {
        return bkUserService.changePassword(password, principal.getName());
    }

    @PreAuthorize("hasAuthority('auditlist')")
    @GetMapping(value = "/audit")
    public Page<BkUser> getAudit(Integer index) {
        return bkUserService.getAuditUserPage(index);
    }

    @PreAuthorize("hasAuthority('passlist')")
    @GetMapping(value = "/pass")
    public Page<BkUser> getPass(Integer index) {
        return bkUserService.getPassUserPage(index);
    }

    @PutMapping(value = "/allowAudit")
    public Boolean allowAudit(Integer userid,Integer pass) {
        return bkUserService.allowAudit(userid,pass);
    }

}
